<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use Markdown;
use App\Model\Art;
use Auth;

use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;

use Captcha;

class ArticleController extends Controller
{


    //验证码方法

    public  function getCode()
    {



       /* $phraseBuilder = new PhraseBuilder(3, '23456789');
        $builder = new CaptchaBuilder();
         $builder->build(200,60);
         $content=$builder->output();
         return response($content)->header('Content-Type', 'image/jpeg');*/
       return  Captcha::src();
        //echo captcha_img();
    }

    public function getVc()
    {
       // echo Captcha::generate();
        dd(Captcha::check('f4bj'));
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       // $arts = DB::table('arts')->orderBy('id','desc')->paginate(2);
     // $art = new Art;
      // $arts = $art->paginate(2);
        $arts = Art::orderBy('id','desc')->paginate(2);
      return view('art.index',['arts'=>$arts]);

        /*//删除数据
        $res = DB::table('arts')->where('id','>',1)->delete();
        dd($res);
        //修改
        $res = DB::table('arts')->where('id',2)->update(['content'=>'update2']);
        dd($res);
        //插入数据
        $art = ['title'=>'2','content'=>'2'];
        $res = DB::table('arts')->insert($art);
        dd($res);
        //查询数据
        $arts = DB::table('arts')->get();
        dd($arts);
        return view('art.index',['title'=>$title]);*/


         //$cats = DB::table('cats')->first();

/*
        $res = DB::table('goods')
            ->leftJoin('cats','goods.cat_id','=','cats.cat_id')
            ->select('goods.goods_name','cats.cat_name')
            ->where('cats.cat_id','=',11)
            ->get();
        dd($res);
        //聚合函数
        //$count = DB::table('goods')->count();
        $max = DB::table('goods')->max('shop_price');

        $res = DB::table('goods')
            ->groupBy('cat_id')
            ->having('cat_id','>',8)
            ->select('cat_id',DB::raw('count(*) as number'))
            ->where('cats.cat_id','=',11)
            ->get();
        dd($res);*/

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('art.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

      // $data = $request->except('_token');

        //$res = DB::table('arts')->insert($data);

        //orm 第一种入库方式 save
       /* $art = new Art();
        $art->title = $request->title;
        $art->content = $request->content;
        $art->save();*/

       //orm第二种入库方式 create
        //$art = Art::create(['title'=>$request->title,'content'=>$request->content]);
        $art = Art::create($request->except('_token'));

        return redirect('/art');


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //$art = DB::table('arts')->where('id','=',$id)->first();
        $art = Art::find($id);

        $art->content= Markdown::convertToHtml($art->content);


       return view('art.show',['art'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //$art = DB::table('arts')->where('id','=',$id)->first();
        $art = Art::find($id);
        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       /*$res = DB::table('arts')->where('id','=',$id)
           ->update(['title'=>$request->title,'content'=>$request->content]);
        */

        //第一种方式
       /* $art = Art::find($id);
        $art->title = $request->title;
        $art->content = $request->content;
        $art->save();
       */
       //第二种方式uodate
        $art = Art::where('id','=',$id)
            ->update($request->except('_token','_method'));

        return redirect('/art');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       //$res = DB::table('arts')->where('id',$id)->delete();

        //第一种删除
        //$art = Art::find($id)->delete();
        //第二种删除
        $art = Art::destroy($id);
        return redirect('/art');
    }


    public function getMk()
    {
        $str="<p>#    h1</p>";
        echo Markdown::convertToHtml($str);
    }

    public  function  getUser()
    {
        dd(Auth::user());

        //dd(Auth::check());

    }
    public function  getLogout()
    {
        Auth::logout();
    }

    public  function postLogin(Request $request)
    {

        $res = Auth::attempt(['email' => $request->email, 'password' => $request->password]);

       if($res){
           return redirect('/art');
       }else{
           return back();
       }
    }


}
